var app = new Vue({
    el: '#app',
    data: {
        form: {
            keywords: '',
            current_page: 1,
            total: 0,
            page_size: 15
        },
        table: {
            list: []
        },
        pageInfo: {
            title: '小区列表'
        },
        dialog: {
            isShow: false,
            title: '',
            disabled: false
        },
        communityDetail: {}
    },
    mounted:function(){
        this.getCommunityList()
    },
    methods: {
        /**
         * 获取小区列表
         */
        getCommunityList: function() {
            var _this = this;
            $.ajax({
                url: '/community/getList',
                type: 'get',
                data: _this.form,
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        _this.table.list = res.data.data;
                        _this.form.current_page = res.data.current_page;
                        _this.form.total = res.data.total;
                    } else {
                        _this.$message.error(res.message)
                    }
                }
            })
        },
        /**
         * 获取小区详情
         */
        getCommunityDetail: function(id, success) {
            var _this = this;
            $.ajax({
                url: '/community/getOne',
                type: 'get',
                data: {id: id},
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        _this.communityDetail = res.data.community;
                        success(res);
                    } else {
                        _this.$message.error(res.message)
                    }
                }
            })
        },
        /**
         * 页面跳转
         * @param val
         */
        handleCurrentChange: function(val) {
            this.form.current_page = val;
            this.getCommunityList();
        },
        /**
         * 小区保存
         */
        communitySave: function(){
            var _this = this;
            if (!_this.communityDetail.name) {
                _this.$message.error('请输入小区名称！')
            } else {
                _this.dialog.disabled = true;
                $.ajax({
                    url: '/community/save',
                    type: 'post',
                    data: _this.communityDetail,
                    dataType: 'json',
                    success: function(res) {
                        if (res.status === 0) {
                            _this.getCommunityList();
                            _this.$message.success(res.message);
                            _this.dialog.isShow = false;
                        } else {
                            _this.$message.error(res.message)
                        }
                        _this.dialog.disabled = false;
                    }
                })
            }

        },
        /**
         * 小区新增
         */
        communityAdd: function() {
            window.location.href = '/community/edit';
        },
        /**
         * 小区编辑
         * @param id
         */
        communityEdit: function(id) {
            window.location.href = '/community/edit?id=' + id;
        },
        /**
         * 小区删除
         * @param row
         */
        communityDel: function(row) {
            var _this = this;
            _this.$confirm('是否删除该小区?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                $.ajax({
                    url: '/community/del',
                    type: 'post',
                    data: {id: row.id},
                    dataType: 'json',
                    success: function(res) {
                        if (res.status === 0) {
                            _this.getCommunityList();
                            _this.$message.success(res.message)
                        } else {
                            _this.$message.error(res.message)
                        }
                    }
                });
            }).catch(function() {
                _this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        /**
         * 上传成功
         */
        fileSuccess: function(response, file, fileList) {
            var _this = this;
            if (response.status === 0) {
                file.id = response.data.id;
                file.raw = {};
                _this.communityDetail.thumb = fileList;
                _this.$message.success(response.message)
            } else {
                _this.$message.error(response.message)
            }
        },
        /**
         * 图片预览
         */
        filePreview: function(file) {
            console.log(file)
        },
        /**
         * 图片删除
         */
        fileRemove: function(file, fileList) {
            this.communityDetail.thumb = [];
        },
        fileExceed: function() {
            this.$message.warning('只允许上传一个文件！');
        }

    }
});
$(document).keyup(function(e){
    var key = e.which;
    if(key==13){
        app.getCommunityList();
        return false;
    }
});
